<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<head>
<jsp:include page="/views/include.jsp"></jsp:include>
<script type="text/javascript">
	var contextPath = '<%=request.getContextPath()%>';
	
	var shopId = 1;
	
    $.parser.onComplete = function() {
		$('body').css('visibility','visible');
		setTimeout(function() {
			$('#loading-mask').remove();
		},50);
    };
    
    $(function(){
    	refresh();
    	
    	$('#dataSource').combobox({
			url : "<c:url value='/etl/mapping_dataSource.tg'/>",
			panelHeight : 'auto',
			valueField : 'dataSource_id',
			textField : 'dataSource_name',
			onSelect : function(rec) {
				$("#table").combobox('clear');
				var url = '<c:url value="/etl/mapping_table.tg"/>?dataSourceId=' + rec.dataSource_id;
				$("#table").combobox('reload', url);
			}
		});
		
		$('#table').combobox({
			panelHeight : 'auto',
			valueField : 'table_id',
			textField : 'table_name',
			onSelect : function(rec) {
				$("#property").combobox('clear');
				var url = '<c:url value="/etl/mapping_property.tg"/>?tableId=' + rec.table_id;
				$("#property").combobox('reload', url);
			}
		});
		
		$('#property').combobox({
			panelHeight : '200',
			valueField : 'property_id',
			textField : 'property_name'
		});
		
		
		$("#covertFactory_add_form").find(".type").change(function() {
			if($(this).val() == 0){
				$("input[name='preValue']").val("");
				$("input[name='behindValue']").val("");
			}else{
				$("input[name='preValue']").val("{excel}");
				$("input[name='behindValue']").val("sql:");
			}
		});
		
		
		
    });
    
    function refresh() {
		$('#dt_covertFactory').datagrid('loadData', {total:0,rows:[]});
		$('#dt_covertFactory').datagrid('load',{
			shopId:shopId
		});
	}
	
	function newItem(){
		$("#covertFactory_add_dialog-buttons a:first-child").show();
		$('#covertFactory_add_dialog').dialog('setTitle','新增转换').dialog('open');
		$('#covertFactory_add_form')[0].reset();
	}
	
	function editItem() {
		var row = $('#dt_covertFactory').datagrid('getSelected');
		if (row) {
			$('#covertFactory_update_form')[0].reset();
			if(row.preValue == '{excel}' || row.preValue == '{date}' ){
				$("#covertFactory_update_form").find("input[type=radio][value=1]").attr("checked",'checked');
				
			}
			$('#covertFactory_update_form').find("input[name='preValue']").val(row.preValue);
			$('#covertFactory_update_form').find("input[name='behindValue']").val(row.behindValue);
			$('#covertFactory_update_form').find("input[id='dataSource']").val(row.dataSource);
			$('#covertFactory_update_form').find("input[name='covertFactoryId']").val(row.id);
			
			$('#covertFactory_update_form').find('#dataSource').combobox('setValue', row.dataSource);
			$('#covertFactory_update_form').find('#dataSource').combobox('disable');
			$('#covertFactory_update_form').find('#table').combobox('setValue', row.table);
			$('#covertFactory_update_form').find('#table').combobox('disable');
			$('#covertFactory_update_form').find('#property').combobox('setValue', row.property);
			$('#covertFactory_update_form').find('#property').combobox('disable');
			$('#covertFactory_update_dialog').dialog('setTitle','更新数据源信息').dialog('open');
		} else {
			$.messager.show({
				title:'注意',
				msg:'请先选择数据，再进行修改。'
			});
		}
	}
	
	function updateItem() {
		$('#covertFactory_update_form').form('submit', {
			url:'<c:url value="/etl/covertFactory_update.tg"/>',
			onSubmit:function() { return true; },
			dataType:"json",
			success:function(result) {
				var data = eval("(" + result + ")");
				if(data.success) {
					$.messager.show({
						title		:	'提示',
						msg			:	data.message,
						showType	:	'slide'
					});
					$('#covertFactory_update_dialog').dialog('close');
					refresh();
				} else {
					$.messager.alert('警告','操作失败！','error');
				}
			}
		});
	}
	
	function removeItem() {
		var row = $('#dt_covertFactory').datagrid('getSelected');
		if (row) {
			if (confirm("数据删除后将无法恢复,确定要删除吗?")) {
				$.ajax({
					typpe			: "post",
					url 			: '<c:url value="/etl/covertFactory_delete.tg"/>',
					dataType		: 'json',
					data			: {"covertFactoryId" : row.id },
					success			: function(result) {
						var data = eval(result);
						if (data.success) {
							$.messager.show({
								title		:	'提示',
								msg			:	data.message,
								showType	:	'slide'
							});
							refresh();
						} else {
							$.messager.alert('警告','操作失败！','error');
						}
					}
				});
			}
		} else {
			$.messager.show({
				title:'注意',
				msg:'请先选择数据，再进行修改。'
			});
		}
	}
	
	
	function checkField(value){
		if(value == 0){
			$("#covertFactory_update_form").find("input[name='preValue']").val("");
			$("#covertFactory_update_form").find("input[name='behindValue']").val("");
		}else{
			$("#covertFactory_update_form").find("input[name='preValue']").val("{excel}");
			$("#covertFactory_update_form").find("input[name='behindValue']").val("sql:");
		}	
	}
	
	function saveItem() {
		$('#covertFactory_add_form').form('submit', {
			url:'<c:url value="/etl/covertFactory_add.tg"/>',
			onSubmit:function() { return true; },
			dataType:"json",
			success:function(result) {
				var data = eval("("+result+")");
				if(data.success) {
					$.messager.show({
						title		:	'提示',
						msg			:	"操作成功!",
						showType	:	'slide'
					});
					$('#covertFactory_add_dialog').dialog('close');
					refresh();
				} else {
					$.messager.alert('警告', data.message ,'error');
				}
			}
		});
	}
	
</script>
</head>
<body style="margin: 0; padding: 0; height: 100%; overflow: hidden; background: #F2FBFF">
	<div id="mainlayout" class="easyui-layout" fit="true">
		<div region="north" border="false">
			<div class="toolbar">
				<table cellpadding="0" cellspacing="0" style="width: 95%;">
					<tr>
						<td><a href="javascript:newItem()" class="easyui-linkbutton" iconCls="icon-add" plain="true">新增</a> 
							<a href="javascript:editItem()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a> 
							<a href="javascript:removeItem()" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">删除</a> 
							<a href="javascript:refresh()" class="easyui-linkbutton" iconCls="icon-reload" plain="true">刷新</a> 
						</td>
					</tr>
				</table>
			</div>
		</div>
		<div region="center" border="false">
			<table id="dt_covertFactory" class="easyui-datagrid" url="<c:url value='/etl/covertFactory_list.tg'/>"  fitColumns="true" fit="true" border="false" pagination="true" striped="true" singleSelect="true" rownumbers="true">
				<thead>
					<th field="shopId" width="45">分公司ID</th>
					<th field="shopName" width="60">分公司名称</th>
					<th field="dataSource" width="80">数据源</th>
					<th field="table" width="100">数据表</th>
					<th field="property" width="80">字段</th>
					<th field="preValue" width="80">值（替换前）</th>
					<th field="behindValue" width="400">值（替换后）</th>
				</thead>
			</table>
		</div>
		<!-- 新增数据源 start -->
		<div id="covertFactory_add_dialog" class="easyui-dialog" closed="true" modal="true" buttons="#covertFactory_add_dialog-buttons">
			<form id="covertFactory_add_form" method="post">
				<div style="padding:20px">
					<table cellpadding="0" cellspacing="0" class="form-table">
						<tr>
							<td>数据源：</td>
							<td><input id="dataSource" class="easyui-combobox" style="width:250px;"></td>
						</tr>
						<tr>
							<td>数据表：</td>
							<td><input id="table" class="easyui-combobox" style="width:250px;"></td>
						</tr>
							<tr>
							<td>字段：</td>
							<td><input id="property" name="propertyId" class="easyui-combobox" style="width:250px"></td>
						</tr>
						<tr>
							<td>转换前值：</td>
							<td><input  name="preValue" class="easyui-validatebox" style="width:250px" /></td>
						</tr>
						<tr>
							<td>转换来源：</td>
							<td>
								静态值<input class="type" name="type" value="0" type="radio" checked="checked" /> &nbsp;
								SQL<input  class="type" name="type" value="1" type="radio" />
							</td>
						</tr>
						<tr>
							<td>转换后值：</td>
							<td><input id="behindValue" name="behindValue" class="easyui-validatebox" style="width:250px" /></td>
						</tr>
						<tr>
							<td colspan="2">
								<span class="addMessage" style="color: red;">
									* 当转换来源为SQL时需以sql:开始,后跟一条sql语句 <br />
									* 当需要使用字段对应的Excel列中的动态值时可使用{excel}代替 <br />
								</span>
							</td>
						</tr>
					</table>
				</div>
			</form>
			<div id="covertFactory_add_dialog-buttons" style="text-align: center">
				<a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="saveItem()">保存</a> 
				<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#covertFactory_add_dialog').dialog('close')">关闭</a>
			</div>
		</div>
		<!-- 新增数据源 end -->
		
		<!-- 修改数据源 start -->
		<div id="covertFactory_update_dialog" class="easyui-dialog" closed="true" modal="true" buttons="#covertFactory_update_dialog-buttons">
			<form id="covertFactory_update_form" method="post">
				<input type="hidden" id=id name="covertFactoryId">
				<div style="padding:20px">
					<table cellpadding="0" cellspacing="0" class="form-table">
						<tr>
							<td>数据源：</td>
							<td><input id="dataSource" class="easyui-combobox" style="width:250px;"></td>
						</tr>
						<tr>
							<td>数据表：</td>
							<td><input id="table" class="easyui-combobox" style="width:250px;"></td>
						</tr>
							<tr>
							<td>字段：</td>
							<td><input id="property" class="easyui-combobox" style="width:250px;"></td>
						</tr>
						<tr>
							<td>转换前值：</td>
							<td><input  id="preValue" name="preValue" class="easyui-validatebox" style="width:250px" /></td>
						</tr>
						<tr>
							<td>转换来源：</td>
							<td>
								静态值<input name="typeEdit" value="0" type="radio" checked="checked" onchange="checkField(this.value)"/> &nbsp;
								SQL<input  name="typeEdit" value="1" type="radio" onchange="checkField(this.value)"/>
							</td>
						</tr>
						<tr>
							<td>转换后值：</td>
							<td><input id="behindValue" name="behindValue" class="easyui-validatebox" style="width:250px" /></td>
						</tr>
						<tr>
							<td colspan="2">
								<span class="addMessage" style="color: red;">
									* 当需要使用字段对应的Excel列中的动态值时可使用{excel}代替 <br />
									* 当转换来源为SQL时转换后值需以sql:开始,后跟一条sql语句 <br />
									&nbsp;&nbsp;&nbsp;&nbsp;如>>  sql:select cust_id from cust_info where cust_name = '{excel}' <br /><br />
								</span>
							</td>
						</tr>
					</table>
				</div>
			</form>
			<div id="covertFactory_update_dialog-buttons" style="text-align: center">
				<a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="updateItem()">保存</a> 
				<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#covertFactory_update_dialog').dialog('close')">关闭</a>
			</div>
		</div>
		<!-- 修改数据源 end -->
	</div>
</body>